﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using DataProcessing;
namespace Gene
{
    /// <summary>
    /// AddPreset.xaml 的交互逻辑
    /// </summary>
    public partial class AddPresetWindow : Window
    {
        public AddPresetWindow(TransformMatrix matrix)
        {
            _MatrixToSave = matrix;
            InitializeComponent();

        }
        TransformMatrix _MatrixToSave;
        
        private void buttonAdd_Click(object sender, RoutedEventArgs e)
        {
            string name = textBoxName.Text;
            if (name.Trim().Equals(""))
            {
                MessageBox.Show("empty name is not allowed");
                return;
            }

            if (AppConfig.Instance.GetPresetPointByName(name) != null)
            {
                MessageBox.Show("preset point with same name already exist");
                return;
            }
            else
            {
                PresetPoint aPoint = new PresetPoint {Name=name,Value=_MatrixToSave };
                AppConfig.Instance.AddPresetPoint(aPoint);
                AppConfig.Instance.Save();
                this.listBoxExistingPresets.ItemsSource = AppConfig.Instance.PresetPoints;
                listBoxExistingPresets.InvalidateVisual();
                Close();
            }
        }


        private void buttonCancel_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            this.listBoxExistingPresets.ItemsSource = AppConfig.Instance.PresetPoints;
        }

        private void buttonDelete_Click(object sender, RoutedEventArgs e)
        {
            if(listBoxExistingPresets.SelectedValue==null)
            {
                MessageBox.Show("you have to choose one preset point to delete");
            }
            PresetPoint selValue = listBoxExistingPresets.SelectedValue as PresetPoint;
            AppConfig.Instance.PresetPoints.Remove(selValue);
            AppConfig.Instance.Save();
            this.listBoxExistingPresets.ItemsSource = null;
            listBoxExistingPresets.ItemsSource= AppConfig.Instance.PresetPoints;
            listBoxExistingPresets.InvalidateVisual();
            listBoxExistingPresets.UpdateLayout();
            this.InvalidateVisual();
        }
        
    }
}
